package com.app.service.impl;

import com.app.mapper.*;
import com.app.vo.ValueAndNameVo;
import com.app.vo.WorkApplyVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.app.common.SystemConstant;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.TokenUtils;
import com.app.vo.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Description:投递简历记录
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class WorkApplyServiceImpl implements WorkApplyService {
    @Autowired
    WorkApplyMapper workApplyMapper;
    @Autowired
    EnterpriseMapper enterpriseMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    JobPublishMapper jobPublishMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<WorkApplyModel> getWorkApplyByCustomer(int customerId, int publishId) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("publish_id", publishId);
        queryWrapper.select().eq("customer_id", customerId);
        List<WorkApplyModel> userList = workApplyMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public List<WorkApplyModel> getWorkApplyByPublishId(int publishId) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("publish_id", publishId);
        List<WorkApplyModel> userList = workApplyMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public int getWorkApplyCountByPublishId(int publishId) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("publish_id", publishId);
        int count = workApplyMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public Page<WorkApplyModel> getWorkApplyByCustomerId(int customerId, int page, int limit) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("customer_id", customerId);
        Page<WorkApplyModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<WorkApplyModel> userList = workApplyMapper.selectPage(pages, queryWrapper);
        return userList;
    }

    @Override
    public Page<WorkApplyModel> getWorkApplyByComId(int comId, int page, int limit) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("e_id", comId);
        Page<WorkApplyModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<WorkApplyModel> userList = workApplyMapper.selectPage(pages, queryWrapper);
        return userList;

    }

    @Override
    public int enterpriseWorkApplyCount(int eid) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("e_id", eid);
        return workApplyMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(WorkApplyModel model) {
        return workApplyMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        workApplyMapper.deleteById(id);
    }

    @Override
    public int countAll(String startTime, String endTime, Integer type) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("distinct publish_id");
        if (type == 1) {
            queryWrapper.apply(" TO_DAYS(crt_time) >= TO_DAYS('" + startTime + "')");
            queryWrapper.apply(" TO_DAYS(crt_time) <= TO_DAYS('" + endTime + "')");
        }
        return workApplyMapper.selectCount(queryWrapper);
    }

    @Override
    public int getModelCountByTime(String crtTime) {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("TO_DAYS(crt_time) = TO_DAYS('" + crtTime + "')");
        return workApplyMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer selectResumeSubmitterCount() {
        List<WorkApplyModel> workApplyModels = workApplyMapper.selectList(null);
        List<WorkApplyModel> arrays = workApplyModels.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                -> new TreeSet<>(Comparator.comparing(WorkApplyModel::getCustomerId))), ArrayList::new));
        return arrays.size();
    }

    @Override
    public Integer selectResumeSubmissionVolumeCount() {
        return workApplyMapper.selectCount(null);
    }

    @Override
    public List<WorkApplyModel> employers() {
        return workApplyMapper.selectEmployers();
    }

    @Override
    public int countGroup() {
        QueryWrapper<WorkApplyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT customer_id");
        return workApplyMapper.selectCount(queryWrapper);
    }

    @Override
    public List<WorkApplyModel> post() {
        return workApplyMapper.selectPost();
    }

    @Override
    public WorkApplyVo resumeList(Integer page, Integer limit, Integer streetId) {
        page = (page - 1) * limit;
        List<WorkApplyModel> workApplyModels = workApplyMapper.selectAll(page, limit);
        Integer count = workApplyMapper.selectCount(null);
        for (int i = 0; i < workApplyModels.size(); i++) {
            QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", workApplyModels.get(i).getCustomerId());
            CustomerModel customerModel = customerMapper.selectOne(queryWrapper);
            QueryWrapper<UserModel> query = new QueryWrapper<>();
            query.eq("c_id", customerModel.getId());
            UserModel model = userMapper.selectOne(query);
            customerModel.setMobile(model.getMobile());
            workApplyModels.get(i).setCustomer(customerModel);
            QueryWrapper<JobPublishModel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id", workApplyModels.get(i).getPublishId());
            JobPublishModel jobPublishModel = jobPublishMapper.selectOne(queryWrapper1);
            workApplyModels.get(i).setJobPublish(jobPublishModel);
            QueryWrapper<EnterpriseModel> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("id", workApplyModels.get(i).getEId());
            EnterpriseModel enterpriseModel = enterpriseMapper.selectOne(queryWrapper2);
            workApplyModels.get(i).setEnterpriseModel(enterpriseModel);
        }
        WorkApplyVo vo = new WorkApplyVo();
        vo.setList(workApplyModels);
        vo.setCount(count);
        return vo;
    }
}
